import 'package:flutter/material.dart';
import 'package:proj_shop/constant/constant.dart';
import 'package:proj_shop/http/weibo/http_weibo.dart';
import 'package:proj_shop/http/weibo/weibo_service_url.dart';
import 'package:proj_shop/roots/weibo/model/FindHomeModel.dart';

class HotSearchPage extends StatefulWidget {
  @override
  _HotSearchPageState createState() => _HotSearchPageState();
}

class _HotSearchPageState extends State<HotSearchPage> {
  bool isShowBlackTitle = false;
  List<Findhottop> mHotSearchList = [];

  @override
  void initState() {
    HttpWeibo().post(
      WeiboServiceUrl.getHotSearchList,
      null,
      (data) {
        mHotSearchList.clear();
        data['data'].forEach((data) {
          mHotSearchList.add(Findhottop.fromJson(data));
        });
        setState(() {});
      },
      (error) {},
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NotificationListener(
        onNotification: (Notification notification) {
          if (notification != null &&
              notification is ScrollUpdateNotification &&
              notification.depth == 0) {
            listenScrollChangeTitle(notification.metrics.pixels);
          }
          return false;
        },
        child: Stack(
          children: [
            CustomScrollView(
              slivers: [
                SliverAppBar(
                  leading: buildSliverLeading(),
                  title: buildIsShowBlackTitle(),
                  floating: false,
                  pinned: true,
                  snap: false,
                  primary: true,
                  expandedHeight: 210.0,
                  backgroundColor: Color(0xffF8F8F8),
                  elevation: 0,
                  centerTitle: true,
                  //是否显示阴影，直接取值innerBoxIsScrolled，展开不显示阴影，合并后会显示
                  //forceElevated: innerBoxIsScrolled,
                  actions: buildSliverActions(),
                  flexibleSpace: buildFlexibleSpaceBar(),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Text buildIsShowBlackTitle() => isShowBlackTitle ? Text('微博热搜') : Text('');

  Container buildSliverLeading() {
    return Container(
      margin: EdgeInsets.only(top: 20, bottom: 10),
      child: isShowBlackTitle
          ? buildSliverBackImage('userinfo_icon_back_black.png')
          : buildSliverBackImage('userinfo_icon_back_white.png'),
    );
  }

  FlexibleSpaceBar buildFlexibleSpaceBar() {
    return FlexibleSpaceBar(
      collapseMode: CollapseMode.pin,
      background: Column(
        children: [
          Container(
            height: 210,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(
                  Constant.ASSETS_IMG + 'hot_search_top.png',
                ),
                fit: BoxFit.fill,
              ),
            ),
            child: Container(),
          )
        ],
      ),
    );
  }

  List<Widget> buildSliverActions() {
    return [
      buildSliverAction(
        'userinfo_search_black.png',
        'userinfo_search_white.png',
      ),
      buildSliverAction(
        'userinfo_more_black.png',
        'userinfo_more_white.png',
      ),
    ];
  }

  Container buildSliverAction(String blackName, String whiteName) {
    return Container(
      margin: EdgeInsets.only(right: 10, top: 20, bottom: 10),
      child: isShowBlackTitle
          ? buildSliverBarActionsImage(blackName)
          : buildSliverBarActionsImage(whiteName),
    );
  }

  Image buildSliverBarActionsImage(String imageName) {
    return Image.asset(
      Constant.ASSETS_IMG + imageName,
      width: 30,
      height: 30,
      fit: BoxFit.cover,
    );
  }

  Image buildSliverBackImage(String name) {
    return Image.asset(
      Constant.ASSETS_IMG + name,
      fit: BoxFit.fitHeight,
    );
  }

  listenScrollChangeTitle(offset) {
    print(offset);
    if (offset > 100) {
      setState(() {
        isShowBlackTitle = true;
      });
    } else {
      setState(() {
        isShowBlackTitle = false;
      });
    }
  }
}
